﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsService _blogNewsService;
        public BlogNewsController(IBlogNewsService blogNewsService)
        {
            _blogNewsService = blogNewsService;
        }

        [HttpGet("BlogNews")]
        public async Task<ApiResult> GetBlogNews()
        {
            var data = await _blogNewsService.QueryAsync();
            if (data == null) return ApiResultHelper.Error("没有更多的值");
            return ApiResultHelper.Success(data);
        }

        [HttpGet("NavigateGetBlogNews")]
        public async Task<ApiResult> NavigateGetBlogNews()
        {
            var data = await _blogNewsService.NavigateQueryAsync();
            if (data == null) return ApiResultHelper.Error("没有更多的值");
            return ApiResultHelper.Success(data);
        }

        [HttpPost("Create")]
        public async Task<ApiResult> Create(BlogNews blogNews)
        {
            bool b = await _blogNewsService.CreateAsync(blogNews);
            if (!b) return ApiResultHelper.Error("添加失败");
            return ApiResultHelper.Success(blogNews);
        }

        [HttpPost("Create2")]
        public async Task<ApiResult> Create(string title, string conent, int typeId)
        {
            BlogNews blogNews = new BlogNews()
            {
                BrowseCount = 0,
                Content = conent,
                Title = title,
                TypeId = typeId,
                WriteId = 1,
                Time = DateTime.Now,
                LikeCount = 0,
            };
            bool b = await _blogNewsService.CreateAsync(blogNews);
            if (!b) return ApiResultHelper.Error("添加失败");
            return ApiResultHelper.Success(blogNews);
        }

        [HttpDelete("Delete")]
        public async Task<ApiResult> Delete(int id)
        {
            bool b = await _blogNewsService.DeleteAsync(id);
            if (!b) return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }

        [HttpPut("Edit")]
        public async Task<ApiResult> Edit(BlogNews blogNews)
        {
            bool b = await _blogNewsService.EditAsync(blogNews);
            if (!b) return ApiResultHelper.Error("更新失败");
            return ApiResultHelper.Success(b);
        }

        [HttpPut("Edit2")]
        public async Task<ApiResult> Edit(int id, string title, string conent, int typeId)
        {
            BlogNews blogNews = await _blogNewsService.FindAsync(id);
            if (blogNews == null) return ApiResultHelper.Error("没有找到文章");

            blogNews.Title = title;
            blogNews.Content = conent;
            blogNews.TypeId = typeId;
            blogNews.Time = DateTime.Now;
            bool b = await _blogNewsService.EditAsync(blogNews);
            if (!b) return ApiResultHelper.Error("更新失败");
            return ApiResultHelper.Success(b);
        }

    }
}
